<?php
namespace Common\Model;

use Think\Model;

class SystemMemberModel extends \BaseModel
{
    /*******用户信息******/
    public function load($user_id)
    {
        $user_info = $this->find($user_id);
        if (!$user_info) return $user_info;
        if (is_array($user_info)) {
           // dump($user_info['headimg']);
            if (strpos($user_info['headimg'], "http") === false && $user_info['headimg']) {
                $user_info['headimg'] = ImgDomain . "/" . $user_info['headimg']."@200h_200w_1e_0c";;
            }elseif(trim($user_info['headimg'])==""){
                $user_info['headimg'] = '/Public/Images/0.jpg';
            }

        }
        $moreField = (array)unserialize($user_info['moreField']);//其它字段
        $user_info = array_merge($moreField, $user_info);
        $gender = dict('gender');
        $user_info['sex_str'] = $gender[$user_info['sex']] ? $gender[$user_info['sex']] : "暂时无填写";
        hook("member_load", $user_info);
        hook("member_load_" . $user_info['type'], $user_info);
        return $user_info;
    }

    /******创建会员**/
    public function create_member($data)
    {
        //	dump($data);exit();
        hook("member_before_save", $data);
        hook("member_before_save_" . $data['type'], $data);
        unset($data['money']);
        unset($data['xuebazi']);
        unset($data['ask_num']);
        $row = $this->create($data);
        $moreField = array_diff($data, $row);
        $row['moreField'] = unserialize($moreField);
        if (!$data['birthday']) unset($data['birthday']);
        $store_id = intval($data['store_id']);
        $channel_id = intval($data['channel_id']);
        if ($user_id = intval($row['user_id'])) {
            $this->where(array('user_id' => $user_id))->save($row);
            hook("member_update", $data);
            hook("member_update_" . $data['type'], $data);
        } else {
            $row['reg_time'] = time();
            $user_id = $this->add($row);
            $data['user_id'] = $user_id;
            if ($user_id) {
                hook("member_insert", $data);
                hook("member_insert_" . $data['type'], $data);
            }
        }
        hook("member_after_save", $data);
        hook("member_after_save_" . $data['type'], $data);
        //dump($this);exit();
        return $user_id;
    }


    /******用户uuid***/
    public function get_uuid($user_id)
    {
        $data = BoxModel("system_member_uuid")->where("user_id='$user_id'")->find();
        $uuid = $data['uuid'];
        if (!$uuid) {
            $uuid = md5($data['user_name'] . "_" . $data['user_id'] . "_" . $data['password']);
            BoxModel("system_member")->where("user_id='$user_id'")->save(array('uuid' => $uuid));
        }
        return $uuid;
    }

    /******使用uuid生成user_id***/
    public function create_member_by_uuid($uuid)
    {
        $user_name = 'user_name_' . $uuid . "_" . great_rand(5);
        $row = array(
            'user_name' => $user_name,
            'password' => md5($user_name),
            'type' => 'BoxEng',
            'from' => 'app',
            'store_id' => 1,
        );
        $user_id = $this->create_member($row);
        $row2 = array(
            'uuid' => $uuid,
            'user_id' => $user_id,
        );
        BoxModel("system_member_uuid")->add($row2);
        return $user_id;
    }

    /*********积分 金额更新*********/
    public function update_statistics($user_id, $field, $num)
    {
        $this->add_field($field);
        $r = $this->where("user_id='$user_id'")->setInc($field, $num);

        return $r;
    }


    /****add_field*****/
    public function add_field($field = "test")
    {
        $sql = "alter table system_member add {$field} int(11)  DEFAULT '0' ";
        $bool = false;
        if (!in_array($field, $this->fields)) {
            $this->query($sql);
            $bool = true;
        }
        if ($bool) {
            $this->flush();
        }
        return $field;
    }


}

